package cn.zbh.user.service.impl;

import cn.zbh.basic.constant.RegisterConstant;
import cn.zbh.basic.exception.BusinessException;
import cn.zbh.basic.mapper.BasicMapper;
import cn.zbh.basic.service.impl.BasicServiceImpl;
import cn.zbh.basic.util.Md5Utils;
import cn.zbh.basic.util.StrUtils;
import cn.zbh.user.domain.Logininfo;
import cn.zbh.user.domain.User;
import cn.zbh.user.dto.UserDto;
import cn.zbh.user.mapper.LogininfoMapper;
import cn.zbh.user.mapper.UserMapper;
import cn.zbh.user.service.IUserService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BasicServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper ;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public void phoneRegister(UserDto dto) {
        String phone=dto.getPhone();
        String password = dto.getPassword();
        String verifiCode = dto.getVerifiCode();
        String qrPassword = dto.getQrPassword();
        //1非空校验
        if(StringUtils.isEmpty(phone)||
                StringUtils.isEmpty(password)||
                StringUtils.isEmpty(verifiCode)||
                StringUtils.isEmpty(qrPassword)
        ){
            throw new BusinessException("参数不能为空");
        }
        //2两次密码不一致校验
        if(!password.equals(qrPassword)){
            throw new BusinessException("两次输入密码不一样");
        }
        //3手机号校验 是否注册
        if(userMapper.getPhone(phone)!=null){
            throw new BusinessException("手机号已被注册");
        }
        //4验证码是否过期
        Object o = redisTemplate.opsForValue().get(phone + RegisterConstant.PHONEREGISTER);

        if(o==null){
            throw new BusinessException("验证码过期");
        }

       String code= o.toString().split(":")[0];
        //5验证码是否正确
        if(!code.equalsIgnoreCase(verifiCode)){
            throw new BusinessException("验证码输入错误");
        }
        //6保存数据 先logininfo 后user
        //通过dto获取user对象
        Logininfo logininfo = dto2Loininfo(dto);
        User user=logininfo2User(logininfo);
        //6.1先保存logininfo表 返自增id
        logininfoMapper.add(logininfo);
        //6.2将自增id对应user表
        user.setLogininfo_id(logininfo.getId());
        userMapper.add(user);



    }

    /**
     * 创建user对象
     *
     * @param logininfo
     * @return
     */

    private User logininfo2User(Logininfo logininfo) {
        User user = new User();
        //copy相同的属性
        BeanUtils.copyProperties(logininfo,user);
        return user;
    }
        /*
        获取logininfo对象
            private String username;
            private String phone;

            private String salt;
            private String password;
            private Integer type;
         */
    public Logininfo dto2Loininfo(UserDto dto){
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setType(1);

        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        //加密加盐
        String password = Md5Utils.encrypByMd5(salt+dto.getPassword());

        logininfo.setSalt(salt);
        logininfo.setPassword(password);


        return logininfo;
    }
}
